package com.kinth.zgwk;


import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20161216.ImageDetectionRequest;
import com.aliyuncs.green.model.v20161216.ImageDetectionResponse;
import com.aliyuncs.green.model.v20161216.ImageDetectionResponse.ImageResult.*;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Arrays;
import java.util.List;
/**
 * 同步图片检测结果调用示例，调用会实时返回检测结果
 */
public class ImageDetection{
	
	public static String imageResult(String imageUrl)
	{
		String result = null;
		//请替换成你自己的accessKeyId、accessKeySecret
    	String accessKey = "LTAIwoDPQKmK5uUQ";
    	String accessCode = "1rny6QCJvBUJ0eOQdATRMLx4Vm5q0y";
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",  accessKey, accessCode);
        IAcsClient client = new DefaultAcsClient(profile);
        ImageDetectionRequest imageDetectionRequest = new ImageDetectionRequest();
        /**
         * 是否同步调用
         * false: 同步
         */
        imageDetectionRequest.setAsync(false);
        imageDetectionRequest.setScenes(Arrays.asList("porn","ocr", "illegal"));
        imageDetectionRequest.setConnectTimeout(4000);
        imageDetectionRequest.setReadTimeout(4000);
        /**
         * 同步图片检测一次只支持单张图片进行检测
         */
       
        String imageUrlKaikai = imageUrl;
        imageDetectionRequest.setImageUrls(Arrays.asList(imageUrlKaikai));
        try {
            ImageDetectionResponse imageDetectionResponse = client.getAcsResponse(imageDetectionRequest);
            //System.out.println(JSON.toJSONString(imageDetectionResponse));
            if("Success".equals(imageDetectionResponse.getCode())){
                List<ImageDetectionResponse.ImageResult> imageResults = imageDetectionResponse.getImageResults();
                if(imageResults != null && imageResults.size() > 0) {
                    //同步图片检测只有一个返回的ImageResult
                    ImageDetectionResponse.ImageResult imageResult = imageResults.get(0);
                    //porn场景对应的检测结果放在pornResult字段中
                    //ocr场景对应的检测结果放在ocrResult字段中
                    //illegal场景对应的检测结果放在illegalResult字段中
                    //ad场景对应的检测结果放在adResult字段中
                    //sensitiveFace场景对应的检测结果放在sensitiveFaceResult字段中
                    //qrcode场景对应的检测结果放在qrcodeResult字段中
                    //请按需获取
                   
                    //其他的场景的类似向黄图检测结果一样去get结果，进行处理
                    /**
                     * ocr识别结果
                     */
                    OcrResult ocrResult = imageResult.getOcrResult();
                    if(ocrResult !=  null) {
                        List<String> texts=  ocrResult.getText();
                        if(texts != null && texts.size() > 0){
                            //for (String text : texts) 
                            {
                            	String text = texts.get(0);
                                result = text;
                            }
                        }
                    }
                   
                }
            }else{
                /**
                 * 检测失败
                 */
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return result;
	}
	
    public static void main(String[] args) {
        
        /**
         * 同步图片检测一次只支持单张图片进行检测
         */
        //http://shop.99114.com/47861114/getContactTel?width=90&height=25&type=1&lineSize=15
        //18660339077
        //imageDetectionRequest.setImageUrls(Arrays.asList("https://img.alicdn.com/tfs/TB1urBOQFXXXXbMXFXXXXXXXXXX-1442-257.png"));
        //weihaidimai@126.com
        //http://shop.99114.com/47861114/getContactTel?width=140&height=25&type=4&lineSize=15
        //http://shop.99114.com/47834675/getContactTel?width=90&height=25&type=1&lineSize=15
        //13383108563
        //http://shop.99114.com/47834675/getContactTel?width=140&height=25&type=4&lineSize=15
        //gang0601163COm
        //http://shop.99114.com/47826404/getContactTel?width=90&height=25&type=1&lineSize=15
        //15188882288
        //http://shop.99114.com/47826404/getContactTel?width=140&height=25&type=4&lineSize=15
        //380798140qqCOm
        //http://shop.99114.com/16645968/getContactTel?width=90&height=25&type=1&lineSize=15
        //1369000000
        //http://shop.99114.com/16645968/getContactTel?width=90&height=25&type=2&lineSize=15
        //8505158376311
        //http://shop.99114.com/16645968/getContactTel?width=140&height=25&type=4&lineSize=15
        //wangchangwan99114Com
        //
    	String imageUrl = "http://shop.99114.com/16645968/getContactTel?width=90&height=25&type=1&lineSize=15";
    	String result = imageResult(imageUrl);
    	System.out.println("测试结果："+result);
    }
}